package nl.mvvenrooij.recommended.twitter;

import nl.mvvenrooij.recommended.twitter.data.Status;
import nl.mvvenrooij.recommended.twitter.data.URLEntity;
import org.testng.annotations.Test;

import java.net.URL;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasItemInArray;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;


public class CanParseTwitterResult {

    @Test(groups = "unit")
    public void textAttributeInResultJsonCanBeParsed() throws Exception {
        Twitter twitter = createTwitterWithStubbedApiThatReturns("[{\"text\":\"some random text\"}]");
        Status expected = new Status();
        expected.setText("some random text");
        assertThat("Twitter api returns correct status with text", twitter.lastTweets(), hasItem(expected));
    }

    @Test(groups = "unit")
    public void entitiesAttributeInResultJsonCanBeParsed() throws Exception {
        Twitter twitter = createTwitterWithStubbedApiThatReturns("[{\"entities\":{\"hashtags\":[],\"urls\":[{\"indices\":[61,79],\"url\":\"http:\\/\\/j.mp\\/kV2bLe\",\"expanded_url\":null}],\"user_mentions\":[]}}]");

        URLEntity expectedUrlEntity = new URLEntity();
        expectedUrlEntity.setUrl(new URL("http://j.mp/kV2bLe"));
        assertThat("Twitter api returns correct status url entity", twitter.lastTweets().get(0).getEntities().getUrls(), hasItemInArray(expectedUrlEntity));
    }

    private Twitter createTwitterWithStubbedApiThatReturns(String toReturn) {
        TwitterApi mockedTwitterApi = mock(TwitterApi.class);
        when(mockedTwitterApi.callTwitterApi()).thenReturn(toReturn);
        return new Twitter(mockedTwitterApi);
    }

}
